package com.huawei.dairy;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * @description:
 * @author: lhh
 * @time: 2022/6/7
 */


        import java.awt.Component;
        import java.awt.event.ItemEvent;
        import java.awt.event.ItemListener;
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;

public class Text {
    public Text() {
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(250, 250);
        jf.setLocationRelativeTo((Component)null);
        jf.setDefaultCloseOperation(3);
        JPanel panel = new JPanel();
        JLabel label = new JLabel("用户：");
        panel.add(label);
        String[] listData = new String[]{"人1", "电脑1", "人2", "电脑2"};
        final JComboBox<String> comboBox = new JComboBox(listData);
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == 1) {
                    System.out.println("选中: " + comboBox.getSelectedIndex() + " = " + comboBox.getSelectedItem());
                }

            }
        });
        comboBox.setSelectedIndex(2);
        panel.add(comboBox);
        jf.setContentPane(panel);
        jf.setVisible(true);
    }
}
